<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
  <head>
    <th:block th:include="include :: header('动态详情')" />
    <style>
      body {
        background: #f5f7fa;
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
          sans-serif;
      }

      .detail-container {
        max-width: 800px;
        margin: 0 auto;
        padding: 20px;
      }

      .detail-card {
        background: white;
        border-radius: 16px;
        box-shadow: 0 2px 16px rgba(0, 0, 0, 0.08);
        margin-bottom: 20px;
        overflow: hidden;
      }

      .detail-header {
        padding: 30px 30px 20px;
        border-bottom: 1px solid #f0f0f0;
      }

      .detail-title {
        font-size: 28px;
        font-weight: 700;
        margin-bottom: 16px;
        color: #333;
        line-height: 1.3;
      }

      .detail-meta {
        display: flex;
        flex-wrap: wrap;
        gap: 20px;
        font-size: 14px;
        color: #666;
      }

      .meta-item {
        display: flex;
        align-items: center;
      }

      .meta-item strong {
        color: #333;
        margin-left: 5px;
      }

      .status-badge {
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 12px;
        font-weight: 500;
        margin-left: 10px;
      }

      .label-warning {
        background: #fff3cd;
        color: #856404;
        border: 1px solid #ffeaa7;
      }

      .label-success {
        background: #d4edda;
        color: #155724;
        border: 1px solid #c3e6cb;
      }

      .label-danger {
        background: #f8d7da;
        color: #721c24;
        border: 1px solid #f5c6cb;
      }

      .detail-content {
        padding: 30px;
        line-height: 1.8;
        font-size: 16px;
        color: #333;
      }

      .detail-content p {
        margin-bottom: 16px;
      }

      .detail-content img {
        max-width: 100%;
        height: auto;
        border-radius: 8px;
        margin: 16px 0;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      }

      .detail-stats {
        padding: 20px 30px;
        background: #f8f9fa;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 14px;
      }

      .stat-item {
        display: flex;
        align-items: center;
        color: #666;
      }

      .stat-item i {
        margin-right: 6px;
        color: #999;
      }

      .comments-section {
        background: white;
        border-radius: 16px;
        box-shadow: 0 2px 16px rgba(0, 0, 0, 0.08);
        overflow: hidden;
      }

      .comments-header {
        padding: 24px 30px;
        border-bottom: 1px solid #f0f0f0;
        font-size: 20px;
        font-weight: 600;
        color: #333;
        display: flex;
        align-items: center;
      }

      .comments-header i {
        margin-right: 8px;
        color: #666;
      }

      .comment-form {
        padding: 24px 30px;
        border-bottom: 1px solid #f0f0f0;
      }

      .comment-form .form-group {
        margin-bottom: 16px;
      }

      .comment-form label {
        display: block;
        margin-bottom: 8px;
        font-weight: 500;
        color: #333;
      }

      .comment-form textarea {
        width: 100%;
        padding: 12px;
        border: 2px solid #e0e0e0;
        border-radius: 8px;
        font-size: 14px;
        resize: vertical;
        transition: border-color 0.2s ease;
      }

      .comment-form textarea:focus {
        border-color: #007bff;
        outline: none;
      }

      .comment-form .btn {
        background: #007bff;
        color: white;
        border: none;
        padding: 10px 20px;
        border-radius: 8px;
        font-size: 14px;
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        gap: 6px;
        transition: background 0.2s ease;
      }

      .comment-form .btn:hover {
        background: #0056b3;
      }

      .comment-list {
        padding: 24px 30px;
      }

      .comment-item {
        display: flex;
        margin-bottom: 20px;
        padding-bottom: 20px;
        border-bottom: 1px solid #f0f0f0;
      }

      .comment-item:last-child {
        border-bottom: none;
        margin-bottom: 0;
        padding-bottom: 0;
      }

      .comment-avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        margin-right: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-weight: 600;
        font-size: 16px;
      }

      .comment-content {
        flex: 1;
      }

      .comment-author {
        font-weight: 600;
        margin-bottom: 6px;
        color: #333;
        display: flex;
        align-items: center;
        gap: 8px;
      }

      .audit-badge {
        font-size: 11px;
        padding: 2px 8px;
        border-radius: 12px;
        font-weight: 500;
        text-transform: uppercase;
      }

      .audit-badge.pending {
        background: #fff3cd;
        color: #856404;
        border: 1px solid #ffeaa7;
      }

      .audit-badge.approved {
        background: #d4edda;
        color: #155724;
        border: 1px solid #c3e6cb;
      }

      .audit-badge.rejected {
        background: #f8d7da;
        color: #721c24;
        border: 1px solid #f5c6cb;
      }

      .comment-text {
        margin-bottom: 8px;
        line-height: 1.6;
        color: #555;
      }

      .comment-time {
        font-size: 12px;
        color: #999;
      }

      .btn-back {
        background: white;
        border: 2px solid #e0e0e0;
        color: #666;
        padding: 10px 20px;
        border-radius: 8px;
        margin-bottom: 20px;
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        gap: 8px;
        text-decoration: none;
        transition: all 0.2s ease;
      }

      .btn-back:hover {
        border-color: #007bff;
        color: #007bff;
        text-decoration: none;
      }

      .empty-comments {
        text-align: center;
        padding: 60px 20px;
        color: #999;
      }

      .empty-comments i {
        font-size: 48px;
        margin-bottom: 16px;
        opacity: 0.3;
      }

      .empty-comments p {
        font-size: 16px;
        margin: 0;
      }

      @media (max-width: 768px) {
        .detail-container {
          padding: 10px;
        }

        .detail-header,
        .detail-content,
        .comment-form,
        .comment-list,
        .comments-header {
          padding-left: 20px;
          padding-right: 20px;
        }

        .detail-meta {
          flex-direction: column;
          gap: 8px;
        }
      }
    </style>
  </head>
  <body class="gray-bg">
    <div class="detail-container">
      <a href="javascript:void(0)" class="btn-back" onclick="goBackToList()">
        <i class="fa fa-arrow-left"></i> 返回列表
      </a>

      <!-- 动态详情 -->
      <div class="detail-card">
        <div class="detail-header">
          <h1 class="detail-title" th:text="${djGraphicDynamic.title}">
            动态标题
          </h1>
          <div class="detail-meta">
            <div class="meta-item">
              <span>发布者：</span>
              <img
                th:if="${djGraphicDynamic.avatar != null and djGraphicDynamic.avatar != ''}"
                th:src="${djGraphicDynamic.avatar}"
                alt="头像"
                style="
                  width: 20px;
                  height: 20px;
                  border-radius: 50%;
                  margin: 0 5px;
                  vertical-align: middle;
                "
              />
              <strong th:text="${djGraphicDynamic.publisher}">发布者</strong>
            </div>
            <div class="meta-item">
              发布时间：<strong
                th:text="${#dates.format(djGraphicDynamic.publishTime, 'yyyy-MM-dd HH:mm:ss')}"
                >发布时间</strong
              >
            </div>
            <div class="meta-item">
              状态：
              <span
                th:if="${djGraphicDynamic.auditFlag == 0}"
                class="status-badge label-warning"
                >待审核</span
              >
              <span
                th:if="${djGraphicDynamic.auditFlag == 1}"
                class="status-badge label-success"
                >已通过</span
              >
              <span
                th:if="${djGraphicDynamic.auditFlag == 2}"
                class="status-badge label-danger"
                >已拒绝</span
              >
            </div>
          </div>
        </div>
        <div class="detail-content">
          <div th:utext="${djGraphicDynamic.content}">富文本内容</div>
        </div>
        <div class="detail-stats">
          <div class="stat-item">
            <i class="fa fa-eye"></i>
            <span
              >浏览量：<strong th:text="${djGraphicDynamic.viewCount}"
                >0</strong
              ></span
            >
          </div>
          <div class="stat-item">
            <i class="fa fa-heart"></i>
            <span
              >点赞量：<strong th:text="${djGraphicDynamic.likeCount}"
                >0</strong
              ></span
            >
          </div>
        </div>
      </div>

      <!-- 评论区域 -->
      <div class="comments-section">
        <div class="comments-header">
          <i class="fa fa-comments"></i> 评论区
          <span
            id="commentsCount"
            style="
              font-size: 14px;
              font-weight: normal;
              color: #666;
              margin-left: 10px;
            "
            >(0条评论)</span
          >
        </div>

        <!-- 评论表单 -->
        <div class="comment-form">
          <div class="form-group">
            <label>发表评论：</label>
            <textarea
              id="commentContent"
              rows="4"
              placeholder="请输入您的评论..."
            ></textarea>
          </div>
          <div class="form-group">
            <button type="button" class="btn" onclick="addComment()">
              <i class="fa fa-send"></i> 发表评论
            </button>
          </div>
        </div>

        <!-- 评论列表 -->
        <div class="comment-list" id="commentList">
          <!-- 评论将通过JavaScript动态加载 -->
        </div>

        <!-- 暂无评论状态 -->
        <div id="emptyComments" class="empty-comments" style="display: none">
          <i class="fa fa-comments-o"></i>
          <p>暂无评论，快来抢沙发吧~</p>
        </div>
      </div>
    </div>

    <th:block th:include="include :: footer" />

    <script type="text/javascript">
      var prefix = ctx + "hui/interact/graphic";
      var dynamicId = [[${djGraphicDynamic.id}]] ;

      $(function () {
        // 页面加载时自动加载评论列表
        loadComments();
      });

      // 返回列表页面
      function goBackToList() {
        window.location.href = prefix;
      }

      // 加载评论列表
      function loadComments() {
        $.ajax({
          url: prefix + "/detail/comments/" + dynamicId,
          type: "POST",
          success: function (result) {
            if (result.code === 0) {
              renderComments(result.data);
            } else {
              console.error("加载评论失败:", result.msg);
              $("#emptyComments").show();
            }
          },
          error: function () {
            console.error("加载评论失败");
            $("#emptyComments").show();
          },
        });
      }

      // 渲染评论列表
      function renderComments(comments) {
        var html = "";
        var approvedComments = [];

        if (comments && comments.length > 0) {
          comments.forEach(function (comment) {
            // 显示所有后端返回的评论（后端已经根据权限过滤）
            approvedComments.push(comment);
            html += generateCommentHTML(comment);
          });

          if (html) {
            $("#commentList").html(html);
            $("#emptyComments").hide();
          } else {
            $("#commentList").html("");
            $("#emptyComments").show();
          }
        } else {
          $("#commentList").html("");
          $("#emptyComments").show();
        }

        // 更新评论数量显示
        var count = approvedComments.length;
        $("#commentsCount").text("(" + count + "条评论)");
      }

      // 生成评论HTML
      function generateCommentHTML(comment) {
        var auditBadge = "";
        var auditClass = "";
        var auditText = "";

        switch (comment.auditFlag.toString()) {
          case "0":
            auditClass = "pending";
            auditText = "待审核";
            break;
          case "1":
            auditClass = "approved";
            auditText = "已审核";
            break;
          case "2":
            auditClass = "rejected";
            auditText = "已拒绝";
            break;
        }

        if (auditText) {
          auditBadge =
            '<span class="audit-badge ' +
            auditClass +
            '">' +
            auditText +
            "</span>";
        }

        var username = comment.username || "匿名用户";
        var firstChar = username.substring(0, 1);
        var commentTime = comment.commentTime
          ? formatDateTime(comment.commentTime)
          : "";

        return `
          <div class="comment-item">
            <div class="comment-avatar">${firstChar}</div>
            <div class="comment-content">
              <div class="comment-author">
                <span>${username}</span>
                ${auditBadge}
              </div>
              <div class="comment-text">${comment.content || ""}</div>
              <div class="comment-time">${commentTime}</div>
            </div>
          </div>
        `;
      }

      // 格式化日期时间
      function formatDateTime(dateStr) {
        if (!dateStr) return "";
        var date = new Date(dateStr);
        if (isNaN(date.getTime())) return dateStr;

        var year = date.getFullYear();
        var month = String(date.getMonth() + 1).padStart(2, "0");
        var day = String(date.getDate()).padStart(2, "0");
        var hours = String(date.getHours()).padStart(2, "0");
        var minutes = String(date.getMinutes()).padStart(2, "0");
        var seconds = String(date.getSeconds()).padStart(2, "0");

        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      }

      // 添加评论
      function addComment() {
        var content = $("#commentContent").val();
        if (!content || content.trim() == "") {
          $.modal.alertWarning("请输入评论内容");
          return;
        }

        var data = {
          dynamicId: dynamicId,
          content: content.trim(),
        };

        $.ajax({
          url: prefix + "/comment/add",
          type: "POST",
          contentType: "application/json",
          data: JSON.stringify(data),
          success: function (result) {
            if (result.code == 0) {
              $.modal.msgSuccess("评论提交成功，等待审核");
              $("#commentContent").val("");
              // 重新加载评论列表
              setTimeout(function () {
                loadComments();
              }, 1000);
            } else {
              $.modal.alertError(result.msg || "评论提交失败");
            }
          },
          error: function () {
            $.modal.alertError("评论提交失败，请重试");
          },
        });
      }
    </script>
  </body>
</html>
